package com.yehuishou.controller;


import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yehuishou.pojo.EnterpriseBack;
import com.yehuishou.service.BackItemService;
import com.yehuishou.service.EnterpriseBackService;


@Controller
public class EnterpriseBackController {

	@Autowired
	private EnterpriseBackService enterpriseBackService;

	private 
	Map<String, Object> map = new HashMap<>();
	
	@RequestMapping("/toEnterprise")
	public String toEnterprise(
			HttpServletRequest req, 
			String npage, 
			String phone,
			String addtime,
			String updatetime,
			String companyname

	) {
	
		EnterpriseBack ep = new EnterpriseBack();
		EnterpriseBack ep2 = new EnterpriseBack();
	
		if(companyname!=null&&companyname!="") {

			ep.setCompanyname(companyname);
			ep2.setCompanyname(companyname);
			
		}
		
		
		
		if(addtime!=null&&addtime!="") {
			
			ep.setAddtime(addtime);
			ep2.setAddtime(addtime);
		}
		
		if(updatetime!=null&&updatetime!="") {
			ep2.setUpdatetime(updatetime);
			ep.setUpdatetime(updatetime);
		}
		Integer page = 1;
		if (npage != null && npage != "") {
			page = Integer.parseInt(npage);
		}

		PageHelper.startPage(page, 10);

		if (phone!= null&&phone!="") {
			ep.setPhone(phone);
			ep2.setPhone(phone);
		}
		PageInfo<EnterpriseBack> pageInfo = new PageInfo<>(enterpriseBackService.selectEnterpriseback(ep));

		req.setAttribute("pageinfo", pageInfo);

		req.setAttribute("searchinfo", ep2);

		return "enterpriseback";
	}

	@RequestMapping("/getOneEnterpriseback")
	public String toEnterpriseInfo(Integer id,HttpServletRequest req) {
		EnterpriseBack ep = new EnterpriseBack();
		ep.setId(id);
     req.setAttribute("ep",enterpriseBackService.selectEnterpriseback(ep).get(0));
		return "enterpriseback_info";
	}
	
	@RequestMapping("/editEnterpriseback")
	public String editEnterpriseInfo(HttpServletRequest req,Integer id) {
		EnterpriseBack ep = new EnterpriseBack();
		ep.setId(id);
     req.setAttribute("ep",enterpriseBackService.selectEnterpriseback(ep).get(0));
		return "enterpriseback_edit";
	}
	
	@ResponseBody
	@RequestMapping("/saveEditEnterprise")
	public Map<String, Object> saveEditEnterpriseInfo(HttpServletRequest req,String npage,Integer id,String companyname,String clientname,String phone,double price) {
		EnterpriseBack ep = new EnterpriseBack();
		ep.setId(id);
		ep.setCompanyname(companyname);
		ep.setClientname(clientname);
		ep.setPhone(phone);
		ep.setPrice(price);
		//ep.setUpdatetime(new Date());
		ep.setStatus(1);//1表示已估价
		//更新数据
		Integer page=1;
		if(npage!=null&&npage!="") {
			page= Integer.parseInt(npage);
		}
		map.put("flag", true);
		enterpriseBackService.updateEnterpriseback(ep);
		return map;
	}
	
	//删除企业回收的信息
	@ResponseBody
	@RequestMapping("/deleteEnterpriseback")
	public Map<String, Object> deleteEnterpriseback(Integer id) {
		enterpriseBackService.deleteOne(id);
		map.put("flag", true);
		
	  return map;
	}
	
	//批量删除
	@ResponseBody
	@RequestMapping("/deleteEnterprisebacks")
	public boolean deleteEnterprisebacks(@RequestParam(value = "td_s[]") Integer[] td_s) {
           System.out.println("-------"); 
           enterpriseBackService.deleteEnters(td_s);
		return true;
		
}  
	
	
}
